import { login, visiblePhoto } from '@/api'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    // 用户名
    name: JSON.parse(localStorage.getItem('loginusers')) || ''
  },
  mutations: {
    // 获取token
    SET_TOKEN: (state, token) => {
      state.token = token
      setToken(token)
    },
    // 获取登录用户
    SET_USER: (state, loginuser) => {
      state.name = loginuser
      localStorage.setItem('loginusers', JSON.stringify(loginuser))
    },
    // 删除token
    REMOVE_TOKEN: (state) => {
      state.token = ''
      // 清除本地name
      state.name = localStorage.setItem('loginusers', '')
      // 清除本地token
      removeToken()
    }
  },
  actions: {
    // 登录
    async loginInfo(store, loginForm) {
      const res = await login(loginForm)
      store.commit('SET_TOKEN', res.data.token)
      store.commit('SET_USER', res.data.userName)
      console.log(res)
      return res.data
    },
    // 获取验证码
    async getImgCode(store, clientToken) {
      const res = await visiblePhoto(clientToken)
      return res
    }
  }
}
